<?php
/**
 * @author Thanhpv
 * @date Wed 03/09/2014
 *
 */
class Watermark extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->library ( 'session' );
	}
	public function index() {
		$this->load->Model ( "mimage" );
		session_start ();
		$userId = "";
		$imageName = "";
		if (isset ( $_SESSION ["userId"] ))
			$userId = $_SESSION ["userId"];
		else
			redirect ( $this->config->item ["RedirectURL"] );
		if (isset ( $_SESSION ["imageName"] )) {
			$imageName = $_SESSION ["imageName"];
			// only accept jpeg file
			$dotPos = strpos ( $imageName, "." );
			$extension = substr ( $imageName, $dotPos, strlen ( $imageName ) );
			if (strcmp ( $extension, ".png" ) == 0) {
				$this->session->set_flashdata ( "Error", "Please use jpeg or jpg extension photo" );
				redirect ( base_url ( "index.php/ImageUpload/ImageUpload/index" ) );
				return;
			}
		} else {
			$_SESSION ["imageName"] = "default.jpg";
			$imageName = $_SESSION ["imageName"];
			// copy default image to userId folder
			$folderName = $userId;
			$destination = './UserUploads/' . $folderName;
			$mode = 0777;
			$mkdirError = ! is_dir ( $destination ) ? mkdir ( $destination, $mode ) : true;
			if (! $mkdirError) {
				$this->session->set_flashdata ( "Error", "There is an error with server" );
				redirect ( base_url ( "index.php/ImageUpload/ImageUpload/index" ) );
				return;
			}
			
			$copyFile = copy ( "./UserUploads/default.jpg", $destination . "/default.jpg" );
			if (! $copyFile) {
				$this->session->set_flashdata ( "Error", "There is an error with server" );
				redirect ( base_url ( "index.php/ImageUpload/ImageUpload/index" ) );
				return;
			}
		}
		// copy original image into wartermark one
		$image = (! isset ( $_SESSION ["isWatermarked"] )) ? 
					(($imageName=="default.jpg")?
							imagecreatefromjpeg ( "./UserUploads/" . $userId . "/" . $imageName ):
							imagecreatefromjpeg($_SESSION["imagePath"])) : 
					((strcmp ( $_SESSION ["isWatermarked"], $imageName ) == 0) ? 
							imagecreatefromjpeg ( $_SESSION["watermarkPath"] ) : 
							imagecreatefromjpeg ( $_SESSION["imagePath"] ) );
		
		if(isset($_SESSION["imagePath"]))
			$size = $this->mimage->getSize($_SESSION["imagePath"]);
		else 
			$size = $this->mimage->getSize("./UserUploads/$userId/default.jpg");
		imagejpeg ( $image, "./UserUploads/" . $userId . "/watermark-" . $imageName );
		$data = array (
				'width' => $size [0],
				'height' => $size [1],
				'userId' => $userId,
				'imageName' => (! isset ( $_SESSION ["isWatermarked"] )) ? ((isset($_SESSION["imagePath"]))?$_SESSION ["imagePath"]:(base_url()."./UserUploads/$userId/default.jpg")): $_SESSION ["watermarkPath"]);
		
		$data["funcType"] = 0;
		$this->load->view ( "FunctionWatermark/index", $data );
	}
	// add watermark to image and save it with name.
	// the same as resize image.
	public function process() {
		$this->load->Model ( "mimage" );
		if (isset ( $_POST )) {
			// back to index
			// check error
			session_start ();
			$this->mimage->watermark ( $_POST );
			redirect ( base_url ( "index.php/FunctionWatermark/Watermark/index" ) );
		}
	}
	
	// save data that user has shared
	// time, privacy, date...
	public function post() {
	}
}